package org.example.modules.user.service.Impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.modules.user.common.Commsult;
import org.example.modules.user.entry.SideJobDetails;
import org.example.modules.user.entry.SideJobMain;
import org.example.modules.user.entry.UserWallet;
import org.example.modules.user.entry.WalletDetails;
import org.example.modules.user.mapper.SideJobDetailsMapper;
import org.example.modules.user.mapper.SideJobMainMapper;
import org.example.modules.user.mapper.UserWalletMapper;
import org.example.modules.user.mapper.WalletDetailsMapper;
import org.example.modules.user.service.SideJobDetailsService;
import org.example.modules.user.service.SideJobMainService;
import org.example.modules.user.service.UserWalletService;
import org.example.modules.user.service.WalletDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @Description SideJobDetailsServiceImpl
 * @Author xiaotian
 * @Date 2025-05-02
 */
@Service
public class SideJobDetailsServiceImpl extends ServiceImpl<SideJobDetailsMapper, SideJobDetails> implements SideJobDetailsService{
    @Autowired
    private SideJobDetailsMapper sideJobDetailsMapper;

    @Autowired
    private SideJobMainService sideJobMainService;
    @Autowired
    private SideJobMainMapper sideJobMainMapper;
    @Autowired
    private UserWalletMapper userWalletMapper;
    @Autowired
    private WalletDetailsMapper walletDetailsMapper;
    @Autowired
    private WalletDetailsService walletDetailsService;

    @Autowired
    private UserWalletService userWalletService;

    @Override
    @Transactional
    public Commsult updateJobDetaisStatus(JSONObject data) {
        Integer id = data.getInteger("acceptId");
        String status = data.getString("status");
        String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        sideJobDetailsMapper.updateJobDetaisStatus(id,status,time);
        SideJobDetails sideJobDetails = sideJobDetailsMapper.selectById(id);
        Integer orderId = data.getInteger("Id");
        if(status.equals("3")){
            SideJobMain sideJobMain =sideJobMainMapper.getSideJobDetails(orderId);
            if(sideJobMain.getType().equals("1")||sideJobMain.getType().equals("2")){
                UserWallet userWallet = userWalletMapper.getWallet(sideJobDetails.getAcceptName());
                userWallet.setBeforeMonry(userWallet.getMoney());
                userWallet.setMoney(userWallet.getMoney().add(BigDecimal.valueOf(sideJobMain.getSolar())));
                userWallet.setUpdateTime(time);
                userWalletMapper.updateById(userWallet);
                WalletDetails walletDetails = new WalletDetails();
                walletDetails.setWalletId(userWallet.getId());
                walletDetails.setType(5);
                walletDetails.setTransactionAmount(BigDecimal.valueOf(sideJobMain.getSolar()));
                walletDetails.setTransactionTime(time);
                walletDetails.setAccountType("订单结算");
                walletDetailsMapper.insert(walletDetails);
            }
        }
        return Commsult.success(sideJobMainService.getSideJobDetails(data));
    }
}
